<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="git," />





  <link rel="alternate" href="/atom.xml" title="AnSwEr's Blog" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="http://o9zpdspb3.bkt.clouddn.com/website_icon.ico?v=5.0.1" />






<meta name="description" content="本手册旨在记录实际工程项目中使用的git命令，方便查找。">
<meta name="keywords" content="git">
<meta property="og:type" content="article">
<meta property="og:title" content="Git速查手册">
<meta property="og:url" content="http://answerywj.com/2016/08/28/Git速查手册/index.html">
<meta property="og:site_name" content="AnSwEr&#39;s Blog">
<meta property="og:description" content="本手册旨在记录实际工程项目中使用的git命令，方便查找。">
<meta property="og:image" content="http://o9zpdspb3.bkt.clouddn.com/git%E5%B7%A5%E4%BD%9C%E6%B5%81.jpg">
<meta property="og:image" content="http://forthebadge.com/images/badges/ages-20-30.svg">
<meta property="og:image" content="https://i.creativecommons.org/l/by-sa/4.0/88x31.png">
<meta property="og:updated_time" content="2017-06-06T09:13:11.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Git速查手册">
<meta name="twitter:description" content="本手册旨在记录实际工程项目中使用的git命令，方便查找。">
<meta name="twitter:image" content="http://o9zpdspb3.bkt.clouddn.com/git%E5%B7%A5%E4%BD%9C%E6%B5%81.jpg">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"always"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: undefined,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://answerywj.com/2016/08/28/Git速查手册/"/>

  <title> Git速查手册 | AnSwEr's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?e12c3989e0050ebd884435d0e1f1bed4";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">AnSwEr's Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Git速查手册
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-08-28T21:42:40+08:00" content="2016-08-28">
              2016-08-28
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/Git/" itemprop="url" rel="index">
                    <span itemprop="name">Git</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
          

          

          
          
             <span id="/2016/08/28/Git速查手册/" class="leancloud_visitors" data-flag-title="Git速查手册">
               &nbsp; | &nbsp;
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               <span class="post-meta-item-text">阅读次数 </span>
               <span class="leancloud-visitors-count"></span>
              </span>
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>本手册旨在记录实际工程项目中使用的git命令，方便查找。</p>
<hr>
<a id="more"></a>
<h2 id="配置git"><a href="#配置git" class="headerlink" title="配置git"></a>配置git</h2><p>笔者使用的是v2.1.0，推荐大家使用v1.8以上的<a href="https://git-scm.com/downloads" target="_blank" rel="external">版本</a>。 查看git版本：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git --version</span></div></pre></td></tr></table></figure></p>
<p>配置命令<code>git config</code>分为三个级别：<br><figure class="highlight routeros"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">--system : 系统级,位于 /etc/gitconfig .</div><div class="line">--global : 用户级,位于 ~/.gitconfig .</div><div class="line">--local : 仓库级,位于 repo/.git<span class="built_in">/config </span>,default并且优先级最高.</div></pre></td></tr></table></figure></p>
<p>首先需要删除global用户信息,防止不同git软件之间的冲突：<br><figure class="highlight routeros"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">$ git<span class="built_in"> config </span>--global --unset user.name</div><div class="line">$ git<span class="built_in"> config </span>--global --unset user.email</div></pre></td></tr></table></figure></p>
<p>设置用户信息.若同时使用gitlab和github,推荐配置local用户信息：<br><figure class="highlight routeros"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">$ git<span class="built_in"> config </span>--local user.name <span class="string">"username"</span></div><div class="line">$ git<span class="built_in"> config </span>--local user.email <span class="string">"email"</span></div></pre></td></tr></table></figure></p>
<p>git支持https和ssh等协议.https除了速度慢以外，还有个最大的麻烦是每次推送都必须输入口令，而ssh支持的原生git协议速度最快。<br>检查本机SSH公钥:<br><figure class="highlight jboss-cli"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ <span class="keyword">ls</span> ~<span class="string">/.ssh</span></div></pre></td></tr></table></figure></p>
<p>若存在,则将<code>id_rsa.pub</code>添加到github的SSH keys中。若不存在,则生成:<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> ssh-keygen -t rsa -C <span class="string">"your_email@youremail.com"</span></span></div></pre></td></tr></table></figure></p>
<p>当ssh配置完成后，再次检查ssh连接情况：<br><figure class="highlight applescript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">$ ssh -T git@github.com</div><div class="line">Hi! You’ve successfully authenticated, <span class="keyword">but</span> GitHub <span class="keyword">does</span> <span class="keyword">not</span> provide shell access.</div></pre></td></tr></table></figure></p>
<p>若出现上述信息，则表示设置成功。<br>若使用https访问, 则进行如下配置,并且设置超时时间避免重复输入密码：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git config --global http.sslverify <span class="literal">false</span></span></div><div class="line"><span class="meta">$</span><span class="bash"> git config --global credential.helper <span class="string">'cache --timeout=3600'</span></span></div></pre></td></tr></table></figure></p>
<p>设置可视化diff和merge工具, linux系统上推荐使用meld或者diffuse：<br><figure class="highlight routeros"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">$ git<span class="built_in"> config </span>--global diff.tool meld</div><div class="line">$ git<span class="built_in"> config </span>--global merge.tool meld</div></pre></td></tr></table></figure></p>
<p>设置颜色,利于使用：<br><figure class="highlight routeros"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ git<span class="built_in"> config </span>--global color.ui.<span class="literal">true</span></div></pre></td></tr></table></figure></p>
<p>设置别名：<br><figure class="highlight routeros"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ git<span class="built_in"> config </span>--global alias checkout co</div></pre></td></tr></table></figure></p>
<blockquote>
<p>上面的命令将<code>checkout</code>设置为别名<code>co</code>。</p>
</blockquote>
<p>最后,查看一下所有的设置：<br><figure class="highlight routeros"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$ git<span class="built_in"> config </span>--local --list</div><div class="line">$ git<span class="built_in"> config </span>--global --list</div><div class="line">$ git<span class="built_in"> config </span>--system --list</div></pre></td></tr></table></figure></p>
<h2 id="工作流"><a href="#工作流" class="headerlink" title="工作流"></a>工作流</h2><p><img src="http://o9zpdspb3.bkt.clouddn.com/git%E5%B7%A5%E4%BD%9C%E6%B5%81.jpg" alt="工作流"></p>
<p>工作区就是你的本地仓库文件夹,不过其中的<code>.git</code>目录不属于工作区,而是版本库。里面存了很多东西，其中最重要的就是称为stage（或者叫index）的暂存区，还有Git为我们自动创建的第一个分支master，以及指向master的一个指针叫HEAD。<br>现在来解释一下前面的添加和提交操作：  </p>
<ol>
<li><code>git add</code>把文件添加进去，实际上就是把文件修改添加到暂存区；</li>
<li><code>git commit</code>提交更改，实际上就是把暂存区的所有内容提交到当前分支。<br>因为我们创建Git版本库时，Git自动为我们创建了唯一一个master分支，所以，现在，git commit就是往master分支上提交更改。</li>
</ol>
<h2 id="基本操作"><a href="#基本操作" class="headerlink" title="基本操作"></a>基本操作</h2><p>获取远程仓库：<br><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ git <span class="keyword">clone</span> <span class="title">git</span>@github.com:USERNAME/repo.git</div></pre></td></tr></table></figure></p>
<p>将本地的仓库添加到远程：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> <span class="built_in">cd</span> repo</span></div><div class="line"><span class="meta">$</span><span class="bash"> git init</span></div><div class="line"><span class="meta">$</span><span class="bash"> git remote add origin git@github.com:USERNAME/repo.git</span></div></pre></td></tr></table></figure></p>
<blockquote>
<p><code>origin</code>为远程仓库。</p>
</blockquote>
<p>添加修改:<br><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$ git <span class="keyword">add</span><span class="bash"> &lt;filename&gt; </span></div><div class="line">$ git <span class="keyword">add</span><span class="bash"> .  <span class="comment"># 添加当前目录所有修改过的文件  </span></span></div><div class="line">$ git <span class="keyword">add</span><span class="bash"> *  <span class="comment"># 递归地添加执行命令时所在的目录中的所有文件</span></span></div></pre></td></tr></table></figure></p>
<p>提交修改:<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git commit -m <span class="string">"commit message"</span></span></div><div class="line"><span class="meta">$</span><span class="bash"> git commit -am <span class="string">"commit message"</span></span></div></pre></td></tr></table></figure></p>
<blockquote>
<p><code>commit message</code>的填写可以参考<a href="http://blog.jobbole.com/92713/" target="_blank" rel="external">写好 Git Commit 信息的 7 个建议</a>。<br><code>am</code>将添加和提交合并为一步,但只对本来就存在的文件有效。</p>
</blockquote>
<p>推送修改：<br><figure class="highlight maxima"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ git <span class="built_in">push</span> -u <span class="built_in">origin</span> &lt;<span class="built_in">feature</span>-branch-name&gt;</div></pre></td></tr></table></figure></p>
<blockquote>
<p><code>-u</code>选项可以将本地分支与远程分支关联,下次<code>git pull</code>操作时可以不带参数.具体参见<a href="http://stackoverflow.com/questions/5697750/what-exactly-does-the-u-do-git-push-u-origin-master-vs-git-push-origin-ma" target="_blank" rel="external">这里</a>。</p>
</blockquote>
<p>查看远程仓库：<br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$ git remote -v</div><div class="line">origin git@github<span class="selector-class">.com</span>:USERNAME/repo<span class="selector-class">.git</span> (push)</div><div class="line">origin git@github<span class="selector-class">.com</span>:USERNAME/repo<span class="selector-class">.git</span> (fetch)</div></pre></td></tr></table></figure></p>
<p>fork后同步上游仓库的更新：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#</span><span class="bash"> 第一次需要添加上游仓库</span></div><div class="line"><span class="meta">$</span><span class="bash"> git remote add upstream git@github.com:USERNAME/repo.git</span></div><div class="line"><span class="meta"> </span></div><div class="line">$<span class="bash"> git remote -v</span></div><div class="line">origin  git@github.com:USERNAME/repo.git (push)</div><div class="line">origin  git@github.com:USERNAME/repo.git (fetch)</div><div class="line">upstream  git@github.com:USERNAME/repo.git  (push)</div><div class="line">upstream  git@github.com:USERNAME/repo.git (fetch)</div><div class="line"><span class="meta"></span></div><div class="line">$<span class="bash"> git fetch upstream </span></div><div class="line"><span class="meta">$</span><span class="bash"> git difftool &lt;branch-name&gt; upstream/master</span></div><div class="line"><span class="meta">$</span><span class="bash"> git merge upstream/master</span></div><div class="line"><span class="meta">$</span><span class="bash"> git mergetool</span></div></pre></td></tr></table></figure></p>
<p>引用公共代码：<br>代码引用在git上有两种方式：<code>submodule</code>和<code>subtree</code>，推荐使用<a href="http://aoxuis.me/post/2013-08-06-git-subtree" target="_blank" rel="external">subtree</a>方式。<br><figure class="highlight armasm"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"># 第一次初始化</div><div class="line">$ git remote <span class="keyword">add </span>-f &lt;remote-<span class="keyword">subtree-repository-name&gt; </span>&lt;remote-<span class="keyword">subtree-repository-url&gt;</span></div><div class="line">$ git <span class="keyword">subtree </span><span class="keyword">add </span>--prefix=&lt;local-<span class="keyword">subtree-directory&gt; </span>&lt;remote-<span class="keyword">subtree-repository&gt; </span>&lt;remote-<span class="keyword">subtree-branch-name&gt; </span>--squash</div><div class="line"></div><div class="line"># 同步<span class="keyword">subtree的更新</span></div><div class="line">$ git <span class="keyword">subtree </span>pull --prefix=&lt;local-<span class="keyword">subtree-directory&gt; </span>&lt;remote-<span class="keyword">subtree-repository&gt; </span>&lt;remote-<span class="keyword">subtree-branch-name&gt; </span>--squash</div><div class="line"></div><div class="line"># 推送到远程<span class="keyword">subtree库</span></div><div class="line">$ git <span class="keyword">subtree </span><span class="keyword">push </span>--prefix=&lt;local-<span class="keyword">subtree-directory&gt; </span>&lt;remote-<span class="keyword">subtree-repository&gt; </span>&lt;remote-<span class="keyword">subtree-branch-name&gt;</span></div></pre></td></tr></table></figure></p>
<h2 id="使用标签"><a href="#使用标签" class="headerlink" title="使用标签"></a>使用标签</h2><p>查看标签 ：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git tag</span></div></pre></td></tr></table></figure></p>
<p>创建标签 ：<br><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">$ git <span class="keyword">tag</span> <span class="title">-a</span> <span class="tag">&lt;tagname&gt;</span> -m <span class="string">"tag message"</span> <span class="comment"># 创建标签在当前最新提交的commit上</span></div><div class="line">$ git <span class="keyword">tag</span> <span class="title">-a</span> <span class="tag">&lt;tagname&gt;</span> -m <span class="string">"tag message"</span> <span class="tag">&lt;commit id&gt;</span> <span class="comment"># 创建标签在指定的commit上</span></div></pre></td></tr></table></figure></p>
<p>推送标签到远程：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git push origin &lt;tagname&gt; <span class="comment"># 推送一个本地标签</span></span></div><div class="line"><span class="meta">$</span><span class="bash"> git push origin --tags <span class="comment"># 推送全部未推送过的本地标签</span></span></div></pre></td></tr></table></figure></p>
<p>删除标签：<br><figure class="highlight elixir"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$ </span>git tag -d &lt;tagname&gt; <span class="comment"># 删除一个本地标签；</span></div><div class="line"><span class="variable">$ </span>git push origin <span class="symbol">:refs/tags/&lt;tagname&gt;</span> <span class="comment"># 删除一个远程标签。</span></div></pre></td></tr></table></figure></p>
<h2 id="撤销与回退"><a href="#撤销与回退" class="headerlink" title="撤销与回退"></a>撤销与回退</h2><p>查看当前仓库状态:<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git status</span></div></pre></td></tr></table></figure></p>
<p>查看文件更改：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git difftool &lt;filename&gt;</span></div><div class="line"><span class="meta">$</span><span class="bash"> git mergetool &lt;filename&gt;</span></div></pre></td></tr></table></figure></p>
<p>查看提交历史:<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git <span class="built_in">log</span></span></div></pre></td></tr></table></figure></p>
<p>撤销工作区的修改：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git checkout -- &lt;filename&gt;</span></div></pre></td></tr></table></figure></p>
<p>撤销所有已经提交到暂存区的修改：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git reset HEAD .</span></div></pre></td></tr></table></figure></p>
<p>撤销提交到暂存区的某个文件或文件夹：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git reset HEAD -filename</span></div></pre></td></tr></table></figure></p>
<p>回退版本，即回退暂存区的修改：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git reset --hard &lt;commit-id&gt;</span></div></pre></td></tr></table></figure></p>
<blockquote>
<p>上一个版本的<code>commit-id</code>可以用<code>HEAD^</code>表示，上上个版本为<code>HEAD^^</code>，以此类推。</p>
</blockquote>
<p>从版本库删除文件：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git rm &lt;filename&gt;</span></div></pre></td></tr></table></figure></p>
<h2 id="分支"><a href="#分支" class="headerlink" title="分支"></a>分支</h2><p>查看所有分支，有<code>*</code>标记的是当前分支：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git branch -a</span></div></pre></td></tr></table></figure></p>
<p>创建本地分支：<br><figure class="highlight haxe"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ git checkout &lt;<span class="keyword">new</span><span class="type">branch</span>&gt;</div></pre></td></tr></table></figure></p>
<p>创建并切换本地分支：<br><figure class="highlight haxe"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ git checkout -b &lt;<span class="keyword">new</span><span class="type">branch</span>&gt;</div></pre></td></tr></table></figure></p>
<p>推送新建本地分支到远程：<br><figure class="highlight gams"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="symbol">$</span> git push -u origin &lt;remote-branch-name&gt;</div><div class="line">  <span class="keyword">or</span></div><div class="line"><span class="symbol">$</span> git push --<span class="keyword">set</span>-upstream <span class="comment">origin &lt;remote-branch-name&gt;</span></div></pre></td></tr></table></figure></p>
<p>删除本地分支：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git branch -d &lt;branch&gt;</span></div></pre></td></tr></table></figure></p>
<blockquote>
<p>若当前分支因为有修改未提交或其它情况不能删除，请使用<code>-D</code>选项强制删除。</p>
</blockquote>
<p>删除远程分支：<br><figure class="highlight maxima"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ git <span class="built_in">push</span> <span class="built_in">origin</span> --<span class="built_in">delete</span> &lt;remote-branch-name&gt;</div></pre></td></tr></table></figure></p>
<p>清除无用的分支：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git remote prune origin</span></div></pre></td></tr></table></figure></p>
<blockquote>
<p>说明：remote上的一个分支被其他人删除后，需要更新本地的分支列表。</p>
</blockquote>
<p>获取远程分支到本地已有分支：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git branch --<span class="built_in">set</span>-upstream &lt;<span class="built_in">local</span>-branch&gt; origin/branch</span></div></pre></td></tr></table></figure></p>
<p>获取远程分支到本地并新建本地分支：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git checkout -b &lt;<span class="built_in">local</span>-branch&gt; &lt;remote-branch&gt;</span></div></pre></td></tr></table></figure></p>
<p>同步当前分支的更新，使用<code>git pull</code>并不保险：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#</span><span class="bash"> 下载最新的代码到远程跟踪分支, 即origin/&lt;branch-name&gt;</span></div><div class="line"><span class="meta">$</span><span class="bash"> git fetch origin &lt;branch-name&gt; </span></div><div class="line"><span class="meta">#</span><span class="bash"> 查看更新内容</span></div><div class="line"><span class="meta">$</span><span class="bash"> git difftool &lt;branch-name&gt; origin/&lt;branch-name&gt;</span></div><div class="line"><span class="meta">#</span><span class="bash"> 尝试合并远程跟踪分支的代码到本地分支 </span></div><div class="line"><span class="meta">$</span><span class="bash"> git merge origin/&lt;branch-name&gt;</span></div><div class="line"><span class="meta">#</span><span class="bash"> 借助mergetool解决冲突              </span></div><div class="line"><span class="meta">$</span><span class="bash"> git mergetool</span></div></pre></td></tr></table></figure></p>
<p>同步其它分支的更新，本例拉取<code>master</code>分支更新：<br><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">$ git fetch origin <span class="keyword">master</span></div><div class="line"><span class="title">$</span> git difftool <span class="tag">&lt;branch-name&gt;</span> origin/<span class="keyword">master</span></div><div class="line"><span class="title">$</span> git merge origin/<span class="keyword">master</span></div><div class="line"><span class="title">$</span> git mergetool</div></pre></td></tr></table></figure></p>
<h2 id="暂存"><a href="#暂存" class="headerlink" title="暂存"></a>暂存</h2><p>当你需要切换分支时,若当前工作区还有些修改没有完成,又不适合提交的,操作切换分支是会提示出错的.这时就需要将这些修改暂存起来:<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git stash save <span class="string">"message"</span></span></div></pre></td></tr></table></figure></p>
<p>查看:<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git stash list</span></div></pre></td></tr></table></figure></p>
<p>恢复:<br><figure class="highlight perl"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$ git stash <span class="keyword">pop</span> [--<span class="keyword">index</span>] [stash@&#123;num&#125;]　</div><div class="line">  <span class="keyword">or</span></div><div class="line">$ git stash apply [--<span class="keyword">index</span>] [stash@&#123;num&#125;]　<span class="comment"># 不删除已恢复的进度.</span></div></pre></td></tr></table></figure></p>
<blockquote>
<p><code>--index</code>表示不仅恢复工作区,还会恢复暂存区;<code>num</code>是你要恢复的操作的序列号,默认恢复最新进度.</p>
</blockquote>
<p>删除进度:<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git stash drop [stash@&#123;num&#125;] <span class="comment"># 删除指定进度</span></span></div><div class="line"><span class="meta">$</span><span class="bash"> git stash clear <span class="comment"># 删除所有</span></span></div></pre></td></tr></table></figure></p>
<h2 id="清理本地仓库"><a href="#清理本地仓库" class="headerlink" title="清理本地仓库"></a>清理本地仓库</h2><ul>
<li><p>检查是否有无用的大文件。</p>
</li>
<li><p>清理无用的分支和标签：</p>
  <figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git branch -d &lt;branch-name&gt;</span></div><div class="line"><span class="meta">$</span><span class="bash"> git tag -d &lt;tag-name&gt;</span></div></pre></td></tr></table></figure>
</li>
<li><p>清理<code>.git</code>目录，一般本地仓库过大都是由于存在过多的<code>loose object</code>:</p>
  <figure class="highlight routeros"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ git gc <span class="attribute">--prune</span>=now</div></pre></td></tr></table></figure>
<blockquote>
<p>tips: 在执行<code>push</code>操作时，<code>git</code>会自动执行一次<code>gc</code>操作，不过只有<code>loose object</code>达到一定数量后才会真正调用，建议手动执行。</p>
</blockquote>
</li>
</ul>
<h2 id="其它"><a href="#其它" class="headerlink" title="其它"></a>其它</h2><p>查看帮助：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git --<span class="built_in">help</span></span></div></pre></td></tr></table></figure></p>
<h3 id="忽略特殊文件"><a href="#忽略特殊文件" class="headerlink" title="忽略特殊文件"></a>忽略特殊文件</h3><p>当你的仓库中有一些文件，类似密码或者数据库文件不需要提交但又必须放在仓库目录下，每次<code>git status</code>都会提示<code>Untracked</code>，看着让人很不爽。解决这个问题只需要在仓库目录创建一个<code>.gitignore</code>文件即可，编写规则如下：<br><figure class="highlight jboss-cli"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">tmp/  <span class="comment"># 忽略tmp文件夹下所有内容</span></div><div class="line">*<span class="string">.ini</span> <span class="comment"># 忽略所有ini文件</span></div><div class="line">!data/ <span class="comment">#忽略除了data文件夹的所有内容</span></div></pre></td></tr></table></figure></p>
<p>当然你不必从头编写<code>.gitignore</code>文件，已经有<a href="https://github.com/github/gitignore" target="_blank" rel="external">模版</a>提供使用了。  </p>
<p>本规则只忽略那些原来没有被track的文件，如果某些文件已经被纳入了版本管理中，则修改<code>.gitignore</code>是无效的。解决方法就是先把本地缓存删除,改变成未track状态：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="meta">$</span><span class="bash"> git rm -r --cached .</span></div></pre></td></tr></table></figure></p>
<h3 id="处理大型二进制文件"><a href="#处理大型二进制文件" class="headerlink" title="处理大型二进制文件"></a>处理大型二进制文件</h3><p>由于git在存储二进制文件时效率不高,所以需要借助<a href="http://www.oschina.net/news/71365/git-annex-lfs-bigfiles-fat-media-bigstore-sym" target="_blank" rel="external">第三方组件</a>。</p>
<h2 id="Reference"><a href="#Reference" class="headerlink" title="Reference"></a>Reference</h2><ol>
<li><a href="http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000" target="_blank" rel="external">廖雪峰老师的git教程</a></li>
<li><a href="http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html" target="_blank" rel="external">常用Git命令清单</a></li>
<li><a href="https://git-scm.com/book/en/v2" target="_blank" rel="external">Git-Book</a></li>
<li><a href="https://git-scm.com/docs" target="_blank" rel="external">Git-Reference</a></li>
<li><a href="https://segmentfault.com/a/1190000002783245" target="_blank" rel="external">Git push与pull的默认行为</a></li>
<li><a href="http://www.tuicool.com/articles/rUBNBvI" target="_blank" rel="external">git stash 详解</a></li>
</ol>
<h2 id="About-me"><a href="#About-me" class="headerlink" title="About me"></a>About me</h2><p><a href="http://forthebadge.com" target="_blank" rel="external"><img src="http://forthebadge.com/images/badges/ages-20-30.svg" alt="forthebadge"></a></p>
<ul>
<li>GitHub：<a href="https://github.com/AnSwErYWJ" target="_blank" rel="external">AnSwErYWJ</a></li>
<li>Blog：<a href="http://www.answerywj.com" target="_blank" rel="external">http://www.answerywj.com</a></li>
<li>Email：<a href="https://mail.google.com" target="_blank" rel="external">yuanweijie1993@gmail.com</a></li>
<li>Weibo：<a href="http://weibo.com/1783591593" target="_blank" rel="external">@AnSwEr不是答案</a></li>
<li>CSDN：<a href="http://blog.csdn.net/u011192270" target="_blank" rel="external">AnSwEr不是答案的专栏</a></li>
</ul>
<p><a rel="external" href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png"></a> This work is licensed under a <a rel="external" href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank">Creative Commons Attribution-ShareAlike 4.0 International License</a>.<br>本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div></div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="http://o9zpdspb3.bkt.clouddn.com/%E5%BE%AE%E4%BF%A1%E8%B5%9E%E8%B5%8F.jpg" alt="AnSwEr(Weijie Yuan) WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="http://o9zpdspb3.bkt.clouddn.com/zhifubaozanshang.jpg" alt="AnSwEr(Weijie Yuan) Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/git/" rel="tag">#git</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/08/10/Hexo+Github博客备份方法/" rel="next" title="Hexo+Github博客备份方法">
                <i class="fa fa-chevron-left"></i> Hexo+Github博客备份方法
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/09/05/Shell脚本清空文件的几种方法/" rel="prev" title="Shell脚本清空文件的几种方法">
                Shell脚本清空文件的几种方法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
  <a class="jiathis_button_tsina"></a>
  <a class="jiathis_button_tqq"></a>
  <a class="jiathis_button_weixin"></a>
  <a class="jiathis_button_cqq"></a>
  <a class="jiathis_button_douban"></a>
  <a class="jiathis_button_renren"></a>
  <a class="jiathis_button_qzone"></a>
  <a class="jiathis_button_kaixin001"></a>
  <a class="jiathis_button_copy"></a>
  <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank"></a>
  <a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
  var jiathis_config={
    hideMore:false
  }
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END -->

      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
            <div id="uyan_frame"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://o9zpdspb3.bkt.clouddn.com/avatar.png"
               alt="AnSwEr(Weijie Yuan)" />
          <p class="site-author-name" itemprop="name">AnSwEr(Weijie Yuan)</p>
          <p class="site-description motion-element" itemprop="description">Stay true to yourself,Stay gold.</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">26</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">7</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">23</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/AnSwErYWJ" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://blog.csdn.net/u011192270" target="_blank" title="CSDN">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  CSDN
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/1783591593" target="_blank" title="Weibo">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                  Weibo
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://gmail.com" target="_blank" title="yuanweijie1993@gmail.com">
                  
                    <i class="fa fa-fw fa-envelope"></i>
                  
                  yuanweijie1993@gmail.com
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-block">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              友情链接
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://xieyufei.com/" title="谢小飞的博客" target="_blank">谢小飞的博客</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#配置git"><span class="nav-number">1.</span> <span class="nav-text">配置git</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#工作流"><span class="nav-number">2.</span> <span class="nav-text">工作流</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基本操作"><span class="nav-number">3.</span> <span class="nav-text">基本操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用标签"><span class="nav-number">4.</span> <span class="nav-text">使用标签</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#撤销与回退"><span class="nav-number">5.</span> <span class="nav-text">撤销与回退</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#分支"><span class="nav-number">6.</span> <span class="nav-text">分支</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#暂存"><span class="nav-number">7.</span> <span class="nav-text">暂存</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#清理本地仓库"><span class="nav-number">8.</span> <span class="nav-text">清理本地仓库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#其它"><span class="nav-number">9.</span> <span class="nav-text">其它</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#忽略特殊文件"><span class="nav-number">9.1.</span> <span class="nav-text">忽略特殊文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#处理大型二进制文件"><span class="nav-number">9.2.</span> <span class="nav-text">处理大型二进制文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Reference"><span class="nav-number">10.</span> <span class="nav-text">Reference</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#About-me"><span class="nav-number">11.</span> <span class="nav-text">About me</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2016 - 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">AnSwEr(Weijie Yuan)</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  



  



  

    
      
    

   
        <!-- UY BEGIN -->
        <script type="text/javascript" src="http://v2.uyan.cc/code/uyan.js?uid=2128235"></script>
        <!-- UY END -->
    
  


  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
       search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();

    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
    'use strict';
    $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
            // get the contents from search data
            isfetched = true;
            $('.popup').detach().appendTo('.header-inner');
            var datas = $( "entry", xmlResponse ).map(function() {
                return {
                    title: $( "title", this ).text(),
                    content: $("content",this).text(),
                    url: $( "url" , this).text()
                };
            }).get();
            var $input = document.getElementById(search_id);
            var $resultContent = document.getElementById(content_id);
            $input.addEventListener('input', function(){
                var matchcounts = 0;
                var str='<ul class=\"search-result-list\">';
                var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                $resultContent.innerHTML = "";
                if (this.value.trim().length > 1) {
                // perform local searching
                datas.forEach(function(data) {
                    var isMatch = true;
                    var content_index = [];
                    var data_title = data.title.trim().toLowerCase();
                    var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                    var data_url = data.url;
                    var index_title = -1;
                    var index_content = -1;
                    var first_occur = -1;
                    // only match artiles with not empty titles and contents
                    if(data_title != '' && data_content != '') {
                        keywords.forEach(function(keyword, i) {
                            index_title = data_title.indexOf(keyword);
                            index_content = data_content.indexOf(keyword);
                            if( index_title < 0 && index_content < 0 ){
                                isMatch = false;
                            } else {
                                if (index_content < 0) {
                                    index_content = 0;
                                }
                                if (i == 0) {
                                    first_occur = index_content;
                                }
                            }
                        });
                    }
                    // show search results
                    if (isMatch) {
                        matchcounts += 1;
                        str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                        var content = data.content.trim().replace(/<[^>]+>/g,"");
                        if (first_occur >= 0) {
                            // cut out 100 characters
                            var start = first_occur - 20;
                            var end = first_occur + 80;
                            if(start < 0){
                                start = 0;
                            }
                            if(start == 0){
                                end = 50;
                            }
                            if(end > content.length){
                                end = content.length;
                            }
                            var match_content = content.substring(start, end);
                            // highlight all keywords
                            keywords.forEach(function(keyword){
                                var regS = new RegExp(keyword, "gi");
                                match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                            });

                            str += "<p class=\"search-result\">" + match_content +"...</p>"
                        }
                        str += "</li>";
                    }
                })};
                str += "</ul>";
                if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
                if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
                $resultContent.innerHTML = str;
            });
            proceedsearch();
        }
    });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };

    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  
  <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
  </script>

  <script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for (i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
    });
  </script>
  <script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("vim4NhVqc5JNcPuljU4r9St0-gzGzoHsz", "k7iiuAIEVRSri40zLM4mhdKt");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

</body>
</html>
